package com.yangzb.flink.sql.sample.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间工具类
 */
public class DateUtil {

    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.sss";
    public static final String DATE_FORMAT_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.sss'Z'";
    public static final String DATE_FORMAT_17 = "yyyyMMddHHmmssSSS";

    public static Date stringToDate(String dateStr, String format) {

        Date currentTime = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            currentTime = formatter.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return currentTime;
    }

    public static String getStringDate(Date date, String format, TimeZone timeZone) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setTimeZone(timeZone);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getStringDate(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 获取指定日期后多少分钟
     *
     * @param date
     * @param minuteSize
     * @return
     */
    public static Date getMinuteAfter(Date date, int minuteSize) {
        return new Date(date.getTime() + minuteSize * 60 * 1000);
    }

    /**
     * 获得指定日期的后指定天
     *
     * @param date
     * @return
     */
    public static Date getDayAfter(Date date, int daySize) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + daySize);
        return c.getTime();
    }

    /**
     * 获得指定日期的前指定天
     *
     * @param date
     * @return
     */
    public static Date getDayBefore(Date date, int daySize) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - daySize);
        return c.getTime();
    }

}
